package com.mjt.admin.controller;


import com.mjt.admin.dto.TreeDto;
import com.mjt.admin.model.RespBean;

import com.mjt.admin.pojo.Menu;
import com.mjt.admin.service.IMenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author 莫某人
 * @since 2021-10-27
 */
@Controller
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private IMenuService menuService;

    /**
     * @description 查询所有菜单
     * @author 莫某人
     * @date 10:47 2021/10/29
     * @param
     * @return {@link }
     **/
    @RequestMapping("/queryAllMenus")
    @ResponseBody
    public List<TreeDto> queryAllMenus(Integer roleId){

        return menuService.queryAllMenus(roleId);
    }



    /**
     * @description 获取菜单列表
     * @author 莫某人
     * @date 20:13 2021/10/31
     * @param
     * @return {@link }
     **/
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> menuList(){
        return menuService.menuList();
    }



    /**
     * @description 菜单管理页面
     * @author 莫某人
     * @date 20:08 2021/10/31
 * @param
     * @return {@link }
     **/
    @RequestMapping("index")
    @PreAuthorize("hasAnyAuthority('1030')")
    public String index(){
        return "/menu/menu";
    }


    /**
     * @description 跳转添加页面
     * @author 莫某人
     * @date 10:14 2021/11/1
 * @param
     * @return {@link }
     **/
    @RequestMapping("/addMenuPage")
    public String addMenuPage(Integer grade,Integer pId,Model model){

        model.addAttribute("grade",grade);
        model.addAttribute("pId",pId);
        return "/menu/add";
    }
    /**
     * @description 添加菜单
     * @author 莫某人
     * @date 10:24 2021/11/1
 * @param null
     * @return {@link }
     **/
    @RequestMapping("/save")
    @ResponseBody
    public RespBean save(Menu menu){
        menuService.saveMenu(menu);

        return RespBean.success("添加成功");
    }

    /**
     * @description 删除菜单
     * @author 莫某人
     * @date 10:35 2021/11/1
     * @return {@link RespBean}
     **/
    @RequestMapping("/delete")
    @ResponseBody
    public RespBean delete(Integer id){
        menuService.delete(id);
        return RespBean.success("删除成功");
    }
    /**
     * @description 打开菜单更新页面
     * @author 莫某人
     * @date 10:47 2021/11/1
 * @param null
     * @return {@link }
     **/
    @RequestMapping("/updateMenuPage")
    public String updateMenuPage(Integer id,Model model){
        model.addAttribute("menu",menuService.getById(id));
        return "menu/update";
    }

    /**
     * @description 更新菜单记录
     * @author 莫某人
     * @date 11:24 2021/11/1
 * @param null
     * @return {@link }
     **/
    @RequestMapping("update")
    @ResponseBody
    public RespBean update(Menu menu){
        menuService.updateMenu(menu);
        return RespBean.success("更新成功");
    }





}
